﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;

namespace SupplyChainManagement
{
    /// <summary>
    /// Form to edit existing product
    /// </summary>
    public partial class ProductEditNext : Form
    {
        List<string> selectedItems;
        ProdEditForm prod;
        List<TextBox> textboxes = new List<TextBox>();
        ProductForm main;

        public ProductEditNext(List<string> selectedItems, ProdEditForm prod, ProductForm main)
        {
            InitializeComponent();
            this.prod = prod;
            this.selectedItems = selectedItems;
            this.main = main;
            LoadData();
        }

        private void LoadData()
        {
            foreach (string s in selectedItems)
            {
                Label l = new Label();
                l.Text = s;
                TextBox t = new TextBox();
                layoutPanel.Controls.Add(l);
                layoutPanel.Controls.Add(t);
                textboxes.Add(t);
            }
            //populate the textboxes
            OdbcConnection dbMySQL = new OdbcConnection(MainForm.strConnect);
            
            try
            {
                dbMySQL.Open();
                int index = 0;
                foreach (string s in selectedItems)
                {
                    
                    OdbcCommand selectItem = dbMySQL.CreateCommand();
                    selectItem.CommandText = "select requirement from contain where inv_name='" + s + "' AND pro_id='" +
                        prod.ProductID + "'";
                    OdbcDataReader sqlReader = selectItem.ExecuteReader();
                    //get the amount of this product
                    string amount = null;
                    while (sqlReader.Read())
                    {
                        amount = sqlReader.GetString(0);
                    }

                    sqlReader.Dispose();
                    textboxes[index].Text = amount;
                    index++;
                }
            }
            catch (OdbcException ex)
            {
                Console.WriteLine("Database error\n\n{0}", ex.ToString());
            }
            finally
            {
                if (dbMySQL != null) dbMySQL.Close();
            }


        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
            prod.Enabled = true;
            prod.Activate();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            prod.SaveToDatabase();
            SaveToDatabase();
            this.Close();
            prod.Close();
            main.LoadData();
            main.Enabled = true;
            main.Activate();
        }

        private void SaveToDatabase()
        {
            OdbcConnection dbMySQL = new OdbcConnection(MainForm.strConnect);

            try
            {
                dbMySQL.Open();
                int index = 0;
                foreach (string s in selectedItems)
                {
                    OdbcCommand selectItem = dbMySQL.CreateCommand();
                    selectItem.CommandText = "select inv_id from inventory where inv_name='" + s + "'";
                    OdbcDataReader sqlReader = selectItem.ExecuteReader();
                    //get the inventory id of this product
                    string id = null;
                    while (sqlReader.Read())
                    {
                        id = sqlReader.GetString(0);
                    }
                    OdbcCommand sqlCommand = dbMySQL.CreateCommand();
                    sqlCommand.CommandText = "update contain set requirement='" + textboxes[index] 
                        + "' where pro_id='" + prod.ProductID + "AND inv_id='" + id + "'";
                    sqlCommand.ExecuteNonQuery();
                    sqlCommand.Dispose();
                }
            }
            catch (OdbcException ex)
            {
                Console.WriteLine("Database error\n\n{0}", ex.ToString());
            }
            finally
            {
                if (dbMySQL != null) dbMySQL.Close();
            }
        }

        private void ProductEditNext_FormClosed(object sender, FormClosedEventArgs e)
        {
            prod.Enabled = true;
            prod.Activate();
        }
    }
}
